package com.zmm.huoqiumaishou.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.zmm.huoqiumaishou.R;
import com.zmm.huoqiumaishou.ui.base.BaseActivity;
import com.zmm.huoqiumaishou.ui.fragment.FragmentFactory;

/**
 * Created by Administrator on 2016/10/19 0019.
 */

public class MainActivity extends BaseActivity {


    @Override
    protected void initView() {
        RadioGroup rg = (RadioGroup) findViewById(R.id.rg_group);
        cerateFragment(1);//默认显示第一个Fragment
        //单选按钮监听
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < group.getChildCount(); i++) {
                    //根据勾选不同的按钮显示不同Fragment
                    if (group.getChildAt(i).getId() == checkedId) {
                        cerateFragment(i);//生成Fragment
                    }
                }
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }
    //生成Fragment的方法
    private void cerateFragment(int i) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment fragment = FragmentFactory.cerateFragment(i);
        transaction.replace(R.id.fl_layout, fragment);
        transaction.commit();
    }
}